package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Goods;
import com.entity.GoodsRepertory;
import com.entity.GoodsRepertoryDetail;
import com.entity.Plan;
import com.entity.PurchaseBill;
import com.entity.Suppiler;
import com.entity.SuppilerAccount;
import com.entity.SuppilerAccountDetail;
import com.service.GoodsRepertoryDetailService;
import com.service.GoodsRepertoryService;
import com.service.GoodsService;
import com.service.PlanService;
import com.service.PurchaseBillService;
import com.service.PurchaseService;
import com.service.SuppilerAccountDetailService;
import com.service.SuppilerAccountService;
import com.service.SuppilerService;

@Controller
public class kucunController {
	
	Map<String, String> map=new HashMap<String, String>();
	@Autowired
	GoodsRepertoryService good;
	
	@Autowired
	GoodsRepertoryDetailService goodsDetail;
	
	@Autowired
	PurchaseBillService purchaseBillService;
	
	@Autowired
	GoodsService goodservice;
	
	@Autowired
	GoodsRepertoryService goodsRepertoryService;
	
	@Autowired
	PurchaseService purchaseService;
	
	@Autowired
	SuppilerAccountService suppilerAccountService;
	
	@Autowired
	PlanService planService;
	
	@Autowired
	SuppilerService suppilerService;
	
	@Autowired
	SuppilerAccountDetailService suppilerAccountDetailService;
	
	
	@RequestMapping("/kucun")
	public String planlist(Model model){
		List<GoodsRepertory> list =good.selectAll();
	model.addAttribute("lists", list);
		return "developer/kucunlist";
	}
	
	@RequestMapping("/tocha")
	public String goodsall(Model model,int gid) {
		List<GoodsRepertoryDetail> list=goodsDetail.selectAll(gid);
		model.addAttribute("lists", list);
		return "developer/kucunview";
	}
	
	@ResponseBody
	@RequestMapping("/delkucun")
	public Map<String, String> del(@RequestParam(value = "id", required = false, defaultValue = "0") int id,
			@RequestParam(value = "gshu", required = false, defaultValue = "0") int gshu) {

		if(gshu<=0) {
			good.deleteByPrimaryKey(id);
			map.put("delResult", "true");
		}else {
			map.put("delResult", "false");
		}
		return map;
	}
	@RequestMapping("/insert")
	public String ruku(int id){
		Date day=new Date();    
		int z =0;
		List<PurchaseBill> purchasebill =purchaseBillService.selectByPrimaryKey(id);
		for (PurchaseBill purchaseBill2 : purchasebill) {
			
		  //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			Goods good =goodservice.selectByPrimaryKey(purchaseBill2.getGoodCount());
			GoodsRepertory goodsRepertory = goodsRepertoryService.selectByPrimaryKeys(good.getgName());
			GoodsRepertoryDetail goodsRepertoryDetail =new GoodsRepertoryDetail();
			
			if(goodsRepertory == null ||"".equals(goodsRepertory)){
				
				GoodsRepertory goodsRepertory1=new GoodsRepertory();
				goodsRepertory1.setCount(0);
				goodsRepertory1.setGoodsname(good.getgName());
				goodsRepertory1.setSafetyCount(100);
				goodsRepertory1.setSuggestprice(100);
				int x = goodsRepertoryService.insert(goodsRepertory1);
				System.err.println(goodsRepertory1.getGoodsId());
				
				goodsRepertoryDetail.setGoodsId(goodsRepertory1.getGoodsId());
				goodsRepertoryDetail.setGoodsname(good.getgName());
				goodsRepertoryDetail.setRucount(purchaseBill2.getGoodCount());
				goodsRepertoryDetail.setShengdata(day);
				goodsRepertoryDetail.setShidata(day);
				goodsRepertoryDetail.setIsreturn(2);
				goodsDetail.insert(goodsRepertoryDetail);
				int xx = goodsRepertoryService.updateCount(goodsRepertory1.getGoodsId());
				if(xx>0){
					z=purchaseService.updateExecute(id);
				}
			}else{
				goodsRepertoryDetail.setGoodsId(goodsRepertory.getGoodsId());
				goodsRepertoryDetail.setGoodsname(good.getgName());
				goodsRepertoryDetail.setRucount(purchaseBill2.getGoodCount());
				goodsRepertoryDetail.setShengdata(day);
				goodsRepertoryDetail.setShidata(day);
				goodsRepertoryDetail.setIsreturn(2);
				goodsDetail.insert(goodsRepertoryDetail);
				int x = goodsRepertoryService.updateCount(goodsRepertory.getGoodsId());
				if(x>0){
					z=purchaseService.updateExecute(id);
				}
			}
			
			
		}
		if(z>0){
			
			Plan plan= planService.selectByPrimaryKey(purchasebill.get(0).getPlanId());
			SuppilerAccount suppilerAccount =new SuppilerAccount();
			suppilerAccount.setRecorddate(day);
			Suppiler suppiler= suppilerService.selectAllName(plan.getPlanHandled());
			suppilerAccount.setSuppilerId(""+suppiler.getSupplierId());
			suppilerAccountService.insert(suppilerAccount);
			int ids = suppilerAccount.getAccountId();
			System.err.println(ids);
//			Integer i =  new Integer (suppilerAccount.getAccountId());
			for (PurchaseBill purchaseBill2 : purchasebill) {
			SuppilerAccountDetail accountDetail =new SuppilerAccountDetail();			
			accountDetail.setAccountId(suppilerAccount.getAccountId());
			accountDetail.setMoney(purchaseBill2.getPrice());
			accountDetail.setRudate(day);
			accountDetail.setStatus(1);
			accountDetail.setBillId(purchaseBill2.getGoodId());
			suppilerAccountDetailService.insert(accountDetail);
			
			}	
			
		}
		return "redirect:kucun";
	}

}
